﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace DidaManager.Models
{
    /// <summary>
    /// DLT645 配置读取与查询
    /// </summary>
    public class Dlt645Config
    {
        /// <summary>
        /// 获取运行目录下 config 目录路径
        /// </summary>
        private static string GetRuntimeConfigDirectory()
        {
            return Path.Combine(AppContext.BaseDirectory, "config");
        }

        /// <summary>
        /// 获取 dlt645.json 的完整路径
        /// </summary>
        private static string GetDlt645ConfigPath()
        {
            return Path.Combine(GetRuntimeConfigDirectory(), "dlt645.json");
        }

        /// <summary>
        /// 读取并反序列化 dlt645.json
        /// </summary>
        private static Dlt645Root LoadConfig()
        {
            string configPath = GetDlt645ConfigPath();
            if (!File.Exists(configPath))
            {
                throw new FileNotFoundException($"未找到配置文件: {configPath}");
            }

            string json = File.ReadAllText(configPath);
            if (string.IsNullOrWhiteSpace(json))
            {
                return new Dlt645Root();
            }

            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            };

            return JsonSerializer.Deserialize<Dlt645Root>(json, options) ?? new Dlt645Root();
        }

        /// <summary>
        /// 获取所有 itemname 列表
        /// </summary>
        public static List<string> GetAllItemNames()
        {
            var config = LoadConfig();
            return config.DLTItems
                .Where(i => !string.IsNullOrWhiteSpace(i.ItemName))
                .Select(i => i.ItemName)
                .Distinct(StringComparer.OrdinalIgnoreCase)
                .ToList();
        }

        /// <summary>
        /// 根据 itemname 获取对应的 type 集合中的 name 列表
        /// </summary>
        public static List<string> GetTypeNamesByItemName(string itemName)
        {
            if (string.IsNullOrWhiteSpace(itemName))
            {
                return new List<string>();
            }

            var config = LoadConfig();
            var item = config.DLTItems
                .FirstOrDefault(i => string.Equals(i.ItemName, itemName, StringComparison.OrdinalIgnoreCase));

            if (item == null || item.Types == null)
            {
                return new List<string>();
            }

            return item.Types
                .Where(t => !string.IsNullOrWhiteSpace(t.Name))
                .Select(t => t.Name)
                .ToList();
        }

        public static string GetDlt645TypeId(string strRuleCategory,string strRuleId)
        {
            var config = LoadConfig();
            var item = config.DLTItems.FirstOrDefault(i => i.ItemName == strRuleCategory);
            if (item == null || item.Types == null)
            {
                return string.Empty;
            }
            var type = item.Types.FirstOrDefault(t => t.Name == strRuleId);
            return type?.Id ?? string.Empty;
        }

        // 数据模型
        private class Dlt645Root
        {
            [JsonPropertyName("DLTItems")]
            public List<DltItem> DLTItems { get; set; } = new List<DltItem>();
        }

        private class DltItem
        {
            [JsonPropertyName("type")]
            public List<DltType> Types { get; set; } = new List<DltType>();

            [JsonPropertyName("itemname")]
            public string ItemName { get; set; } = string.Empty;
        }

        private class DltType
        {
            [JsonPropertyName("name")]
            public string Name { get; set; } = string.Empty;

            [JsonPropertyName("id")]
            public string Id { get; set; } = string.Empty;
        }
    }
}
